<script>
import hotel from "./hotel.vue";
import {data} from "autoprefixer";
import {h} from "vue";

export default {
  computed: {
    hotel() {
      this.edithotel=hotel
      return hotel
    }
  },
  data() {
    return {
      Name:'',
      imageUrl: '',
      edithotel: {
      },
      city:[{

      }],
      cityId: 0,
      hotelId:0,
    }
  },
  mounted() {
    var cityId=this.$route.query.cityId;
    var hotelId=this.$route.query.hotelId;
   this.cityId=cityId
    this.hotelId=hotelId
      this.findOne();
    this.findAllCity()
  },
  methods: {

    onSubmit() {
      this.$axios.post('/AdminHotel/update',this.edithotel).then(data=>{
        if (data.data.code==200){
          this.$message({
            message: '修改成功',
            type: 'success'
          });
          this.$router.push('/hotel')
        }
      })
    },
    findAllCity(){
      this.$axios.get('/city/findallcitys').then(data=>{
        this.city=data.data.data
      })

    },
    findOne(hid){
      this.$axios.get('/hotel/findonehotel',{
        params:{
          cityId:this.cityId,
          hotelId:this.hotelId
        }
      })
        .then(data=>{
          console.log(data.data.data)
          this.edithotel=data.data.data

        })

    },

    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw);
      this.edithotel.hotelPic=file.response.data;
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg';
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!');
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
      }
      return isJPG && isLt2M;
    },
  }
}
</script>

<template>
  <div>


    <el-form ref="edithotel" :model="edithotel" label-width="80px">
      <el-form-item label="酒店名称">
        <el-input v-model="edithotel.hotelName"></el-input>
      </el-form-item>
      <el-form-item label="城市">
        <el-select v-model="edithotel.cityName" placeholder="请选择所属城市">
          <el-option v-for="c in city" :label="c.cityName" :value="c.cityId"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="酒店价格">
        <el-input v-model="edithotel.hotelPrice"></el-input>
      </el-form-item>
      <el-form-item label="酒店描述">
        <el-input v-model="edithotel.hotelComment"></el-input>
      </el-form-item>
      <el-form-item label="酒店信息">
        <el-input v-model="edithotel.hotelInfo"></el-input>
      </el-form-item>
      <el-form-item label="酒店星级">
        <el-input v-model="edithotel.hotelScore"></el-input>
      </el-form-item>
      <el-form-item label="酒店距离">
        <el-input v-model="edithotel.hotelLebgth"></el-input>
      </el-form-item>
      <el-form-item label="图片">
        <el-image
          style="width: 100px; height: 100px"
          :src="edithotel.hotelPic"
          fit="fill"></el-image>
        <el-upload
          class="avatar-uploader"
          action="http://localhost:8081/upload"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload">
          <img v-if="imageUrl" :src="imageUrl" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>


      <el-form-item>
        <el-button type="primary" :plain="true" @click="onSubmit">立即修改</el-button>
        <el-button @click="$router.push('/hotel')">取消</el-button>
      </el-form-item>
    </el-form>

  </div>

</template>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}

</style>
